/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agent;

import agent.keskkonna.KeskkonnaAgent;
import agent.ontology.AlertOntology;
import agent.params.LocationParam;
import agent.params.PassangerParam;
import agent.teavitus.TeavitusAgent;
import agent.teekonna.TeekonnaAgent;
import agent.types.AlertLocationType;
import agent.util.AgentUtil;
import jade.core.Agent;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.util.Logger;
import jade.wrapper.AgentController;
import jade.wrapper.PlatformController;
import java.util.logging.Level;

/**
 *
 * @author margus
 */
public class MainAgent extends Agent {

	private static final long serialVersionUID = 1L;
	private static final Logger LOG = Logger.getJADELogger(MainAgent.class.getName());
    private static AgentController teavitusController;
    private static AgentController keskkonnaController;
    private static AgentController teekonnaController;

    @Override
    protected void setup() {
        super.setup();
        AgentUtil.registerByName(this, AgentType.MAIN);
        try {
            PlatformController container = getContainerController(); // get a container controller for creating new agents
            teavitusController = container.createNewAgent(AgentUtil.getAgentLocalName(TeavitusAgent.class), TeavitusAgent.class.getName(), null);
            teavitusController.start();

            keskkonnaController = container.createNewAgent(AgentUtil.getAgentLocalName(KeskkonnaAgent.class), KeskkonnaAgent.class.getName(), null);
            keskkonnaController.start();

            teekonnaController = container.createNewAgent(AgentUtil.getAgentLocalName(TeekonnaAgent.class), TeekonnaAgent.class.getName(), null);
            teekonnaController.start();

            addBehaviour(new SimpleBehaviour() {
				
				private static final long serialVersionUID = 1L;

				@Override
				public boolean done() {
					return false;
				}
				
				@Override
				public void action() {
					ACLMessage msg = myAgent.receive();
					if (msg == null) {
						block();
					}else {
						try{
							SendType sendType = SendType.valueOf(msg.getOntology());
							
							switch (sendType) {
							case SEND_TEST_ALERT:
								sendAlert();
								break;

							default:
								break;
							}
							
						}catch(Exception ex){
							LOG.log(Level.SEVERE, ex.getMessage(), ex);
						}
					}
				}
			});
            
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    private void sendAlert(){
    	ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
    	msg.setOntology(AlertOntology.ALERT_KONTTROLL_PEATUSES.name());
    	msg.addUserDefinedParameter(PassangerParam.PASSANGER_ID.name(), "1");
    	msg.addUserDefinedParameter(LocationParam.LOCATION_TYPE.name(), AlertLocationType.BUS_STOP.name());
    	msg.addReceiver(AgentUtil.createLocalAid(TeavitusAgent.class));
    	this.send(msg);
    }
}
